<template>
  <Card v-show="currentState === 'edit' || currentState === 'add'">
    <!--表单-->
    <p slot="title">
      <Icon :type="icon"></Icon>
      {{title}}
    </p>
    <Form ref="formItem" :model="formItem" :rules="ruleValidate" :label-width="80">
      <Row :gutter="16">
        <Col span="12">
          <Form-item label="用户名" prop="username">
            <Input type="text" v-model="formItem.username"></Input>
          </Form-item>
        </Col>
        <Col span="12">
          <Form-item label="姓名" prop="name">
            <Input type="text" v-model="formItem.name"></Input>
          </Form-item>
        </Col>
      </Row>
      <Row :gutter="16">
        <Col span="12">
          <Form-item label="密码" prop="password">
            <Input type="password" v-model="formItem.password"></Input>
          </Form-item>
        </Col>
        <Col span="12">
          <Form-item label="确认密码" prop="pwdCheck">
            <Input type="password" v-model="formItem.pwdCheck"></Input>
          </Form-item>
        </Col>
      </Row>
      <Row :gutter="16">
        <Col span="12">
          <Form-item label="邮箱" prop="email">
            <Input type="email" v-model="formItem.email"></Input>
          </Form-item>
        </Col>
        <Col span="12">
          <Form-item label="手机" prop="mobile">
            <Input type="text" v-model="formItem.mobile" number></Input>
          </Form-item>
        </Col>
      </Row>
      <Row :gutter="16">
        <Col span="12">
          <Form-item label="角色" prop="role">
            <CheckboxGroup v-model="formItem.roleIds">
              <Checkbox v-for="(item, index) of roles" :label="roles[index].roleId" :key="item.roleId">
                <span>{{roles[index].remark}}</span>
              </Checkbox>
            </CheckboxGroup>
          </Form-item>
        </Col>
      </Row>
      <Row :gutter="16">
        <Col offset="9" span="4">
          <Form-item>
            <Button type="primary" @click="saverOrUpdate">保存</Button>
            <Button @click="back" style="margin-left: 8px">返回</Button>
          </Form-item>
        </Col>
      </Row>
    </Form>
  </Card>
</template>

<script>
import { saveOrUpdateSysUserInfo, info } from '../../../api/sys/user'
import { getRoles } from '../../../api/sys/role'

export default {
  name: 'user-save-update',
  data () {
    const validateNewPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('新密码不能为空'))
      } else if (value.length < 6) {
        callback(new Error('新密码不能少于6位数'))
      } else {
        if (this.formItem.pwdCheck !== '') {
          // 对第二个密码框单独验证
          this.$refs['formItem'].validateField('pwdCheck')
        }
        callback()
      }
    }
    const validatePassCheck = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('确认密码不能为空'))
      } else if (value !== this.formItem.password) {
        callback(new Error('两次密码不一致'))
      } else {
        callback()
      }
    }
    return {
      currentState: '',
      roles: [],
      formItem: {},
      ruleValidate: {
        username: [
          { required: true, message: '登录账号不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { validator: validateNewPass, trigger: 'blur' }
        ],
        pwdCheck: [
          { required: true, message: '确认密码不能为空', trigger: 'blur' },
          { validator: validatePassCheck, trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    title () {
      return this.currentState === 'edit' ? '编辑用户' : (this.currentState === 'add' ? '新增用户' : '')
    },
    icon () {
      return this.currentState === 'edit' ? 'md-create' : (this.currentState === 'add' ? 'md-add' : '')
    }
  },
  methods: {
    saverOrUpdate () {
      this.$refs['formItem'].validate((valid) => {
        if (valid) {
          saveOrUpdateSysUserInfo(this.formItem).then(res => {
            // this.init()
            this.currentState = 'normal'
            // 添加成功
            this.$emit('showTable')
          })
        }
      })
    },
    back () {
      this.currentState = 'normal'
      this.$emit('showTable')
    },
    showAdd () {
      this.formItem = {}
      getRoles().then(res => {
        this.roles = res.roles
        this.currentState = 'add'
        this.$emit('hideTable')
      })
    },
    showEdit (id) {
      info(id).then(res => {
        this.formItem = res.user
        this.roles = res.roles
        this.currentState = 'edit'
        this.$emit('hideTable')
      })
    }
  }
}
</script>

<style scoped>

</style>
